package com.michaelbulava.TFS.ui.dialogs.connect;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.persistence.IdeaPersistenceStoreProvider;
import com.michaelbulava.TFS.tasks.connect.ConnectTask;
import com.michaelbulava.TFS.ui.controls.connect.ConnectionErrorControl;
import com.michaelbulava.TFS.ui.controls.connect.ServerSelectControl;
import com.michaelbulava.TFS.ui.controls.connect.TeamProjectSelectControl;
import com.michaelbulava.TFS.ui.dialogs.BaseDialog;
import com.michaelbulava.TFS.ui.helpers.IconHelper;
import com.microsoft.tfs.core.TFSTeamProjectCollection;
import com.microsoft.tfs.core.clients.versioncontrol.Workstation;
import com.microsoft.tfs.core.clients.versioncontrol.exceptions.VersionControlException;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

/**
 * Created by MichaelA on 10/6/2015.
 */
public class ReconnectDialog extends BaseDialog {
    private DialogPanel control;

    public ReconnectDialog(@Nullable Project project, String errorMessage) {
        super(project);
        init();
//        if (control == null)
//            createControl();
        control.projectSelectControl.setProject(project);
        control.errorControl.setMessage(errorMessage);
        setOKActionEnabled(false);
    }

    @Override
    protected Action[] createActions(){
        return this.getHelpId() == null?(SystemInfo.isMac?new Action[]{this.getCancelAction(), this.getOKAction()}:new Action[]{this.getOKAction(), this.getCancelAction()}):(SystemInfo.isMac?new Action[]{this.getHelpAction(), this.getCancelAction(), this.getOKAction()}:new Action[]{this.getOKAction(), this.getCancelAction(), this.getHelpAction()});
    }

    @Override
         protected JComponent createControl() {
        if(control == null)
            control = new DialogPanel();
        return control;
    }

    @Override
    protected String dialogTitle() {
        return Messages.getString("ReconnectDialog.Title");
    }


//    /**
//     * Factory method. It creates panel with dialog options. Options panel is located at the
//     * center of the dialog's content pane. The implementation can return <code>null</code>
//     * value. In this case there will be no options panel.
//     *
//     * @return center panel
//     */
//    @Nullable
//    @Override
//    protected JComponent createCenterPanel() {
//        if(control == null)
//            control = new DialogPanel();
//        return control;
//    }

    @Override
    protected void doOKAction(){
        if (control.projectSelectControl.getTeamProjectCollection() == null) {
            Messages.showInfoMessage(control, Messages.getString("ReconnectDialog.CollectionNotSelected_Message"),
                    Messages.getString("ReconnectDialog.CollectionNotSelected_Title"));
            return;
        }
        TFSTeamProjectCollection collection = control.projectSelectControl.getTeamProjectCollection();
        Workstation.getCurrent(IdeaPersistenceStoreProvider.INSTANCE)
                .updateWorkspaceInfoCache(collection.getVersionControlClient()
                        , collection.getAuthorizedTFSUser().toString());
        super.doOKAction();
    }

    private class DialogPanel extends JPanel{
        private ConnectionErrorControl errorControl;
        private TeamProjectSelectControl projectSelectControl;

        public DialogPanel(){
            GridLayoutManager layoutManager = new GridLayoutManager(2,1);
            layoutManager.setMargin(new Insets(4,4,4,4));
            setLayout(layoutManager);

            errorControl = new ConnectionErrorControl();
            //errorControl.setMessage(errorMessage);

            add(errorControl, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                    GridConstraints.FILL_BOTH,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                    new Dimension(450, 85),
                    new Dimension(450, 100),
                    null,
                    0, false));

//            final Spacer spacer1 = new Spacer();
//            add(spacer1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
//                    GridConstraints.FILL_VERTICAL, 1,
//                    GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_CAN_SHRINK,
//                    null,
//                    new Dimension(1, 100),
//                    new Dimension(1, 100),
//                    0, false));

            projectSelectControl = new TeamProjectSelectControl();
            //projectSelectControl.setProject(project);

            add(projectSelectControl, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                    GridConstraints.FILL_BOTH,
                    GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_CAN_SHRINK,
                    GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_CAN_SHRINK,
                    new Dimension(450, 275), //Minimum
                    new Dimension(250, 250), //preferred
                    null, //Maximum
                    0, false));

            projectSelectControl.setBackgroundProcessListener(new TeamProjectSelectControl.BackgroundProcessListener() {
                @Override
                public void processStarted() {
                    ReconnectDialog.this.getWindow().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                }

                @Override
                public void processCompleted() {
                    ReconnectDialog.this.getWindow().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                }
            });

            projectSelectControl.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    setOKActionEnabled(projectSelectControl.getTeamProjectCollection() != null);
                }
            });
        }
    }
}
